/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"

#include "pin_mux.h"
#include <stdbool.h>
/*******************************************************************************
 * Definitions
 ******************************************************************************/

#include "los_config.h"
#include "los_task.h"
#include "los_debug.h"
#include "los_interrupt.h"
#include "los_tick.h"
#include "uart_if.h"


/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/

void delay(unsigned int ticks)
{
    unsigned int magic = 0;
    unsigned int i;
    
    for (i = 0; i < ticks; i++) {
        magic += 1;
    }
    PRINTF("magic = %u\n", magic);
}    

VOID LedSampleEntry(VOID)
{
    while (1) {
        LOS_TaskDelay(200); 
        GPIO_TogglePinsOutput(GPIO, 3, (1 << 11));
    }
}

#define UART_GREETING_STR "UartSampleEntry: hello uart!\n"
VOID UartSampleEntry(VOID)
{
    static DevHandle uart = NULL;

    uart = UartOpen(1);
    if (uart == NULL) {
        PRINTF("UartSampleEntry: open uart 1 failed\n");
        return;
    }
    while (1) {
        LOS_TaskDelay(2000); 
        UartWrite(uart, UART_GREETING_STR, sizeof(UART_GREETING_STR)); 
    }
}


VOID InitLedDemoForLOS(VOID)
{   
    UINT32 uwRet;
    UINT32 taskID1;
    UINT32 taskID2;
    TSK_INIT_PARAM_S stTask = {0};

    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)LedSampleEntry;
    stTask.uwStackSize = 0x500;
    stTask.pcName = "LedSampleEntry";
    stTask.usTaskPrio = 6; // Os task priority is 6
    uwRet = LOS_TaskCreate(&taskID1, &stTask);
    if (uwRet != LOS_OK) {
        printf("Task1 create failed\n");
    }

    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)UartSampleEntry;
    stTask.uwStackSize = 0x500;
    stTask.pcName = "UartSampleEntry";
    stTask.usTaskPrio = 7; // Os task priority is
    uwRet = LOS_TaskCreate(&taskID2, &stTask);
    if (uwRet != LOS_OK) {
        printf("Task2 create failed\n");
    }
}

#include "devmgr_service_start.h"
typedef void (*tcpip_init_done_fn)(void *arg);
extern void tcpip_init(tcpip_init_done_fn initfunc, void *arg);

/*!
 * @brief Main function
 */
int main(void)
{
    gpio_pin_config_t led_config = {
        kGPIO_DigitalOutput, 0,
    };

    /* Init board hardware. */
    /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
    CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);

    BOARD_InitPins();
    BOARD_BootClockPLL180M();
    BOARD_InitDebugConsole();
    
    GPIO_PinInit(GPIO, 2, 0, &led_config);
    GPIO_WritePinOutput(GPIO, 2, 0, 1);
    GPIO_PinInit(GPIO, 3, 11, &led_config);
    GPIO_WritePinOutput(GPIO, 3, 11, 0);

    PRINTF("hello world.\r\n");

    /* Kernel initialization */
    if (LOS_KernelInit() != LOS_OK) {
        return -1;
    }

    printf("\ntcpip_init start\n");
    tcpip_init(NULL, NULL);
    printf("\ntcpip_init end\n");

    /* HDF ENTRY START */
    if (DeviceManagerStart() != 0) {
        PRINTF("HDF START FAIL!\n");
    } else {
        PRINTF("HDF START OK!\n");
    }

    // start ohos l0 kernel
    InitLedDemoForLOS();
    LOS_Start();

    /*while (1)
    {
        delay(0x2000 * 0x2000);
        GPIO_TogglePinsOutput(GPIO, 3, (1 << 11));
    }
*/
}
